#include "header.h"
//BOARD OBJECT SECTION
board::board() //Does nothing
{

}

void board::make() //Makes a board
{
  bool hallways[10][10];
  numrooms=0;
  for (int n=0;n<10;n++)
  {
    debugint("------------------------new room------------------------",n);
  buildRoom();
  }
  
  /*for (int n=0;n<9;n++)
  {
    for (int nn=0;nn<(9-n);nn++)
    {
      debug("--------------------------new hallway -----------------------");
      hallways[n][nn] = buildHallway(rooms[n],rooms[n+nn]);
    } // end nn for
  } // end n for*/
}// end make function

void board::buildRoom() //Builds a random room on the current board
{
  bool roomsclear[20];
  bool roomtotallyclear=false;
  char temp[1];
  do //Doesn't exit this if the room isn't clear
  {
      debug("Executing do while loop");
    rooms[numrooms].x=rand()%70+10;
    rooms[numrooms].y=rand()%70+10;
    rooms[numrooms].w=rand()%12 + 8;
    rooms[numrooms].h=rand()%12 + 8;
      debugint("new room x", rooms[numrooms].x);
      debugint("new room y", rooms[numrooms].y);
      debugint("new room w", rooms[numrooms].w);
      debugint("new room h", rooms[numrooms].h);
      
    room temproom;
    for (int n=0;n<numrooms;n++) //Loops through the rooms array
    {
        debugint("Executing for loop execution #",n);
      temproom = doRoomsCollide(rooms[numrooms],rooms[n]); //Temproom holds the rectangle of collision. 
        debugint("collision room x", temproom.x);
        debugint("collision room y", temproom.y);
        debugint("collision room w", temproom.w);
        debugint("collision room h", temproom.h);
      if (temproom.x==-1) //If this is true, it passed the collision detect function
      {
          roomsclear[n] = true;
          debug("Passed collision check");
      }
      else
      { //This needs to identify which direction to shrink the current room, then do so and run the collision check again.
        //If it fails the collision this time, just roomclear = false and rerand it.
        
          debug("failed collision check, rerandomizing room");          
          roomsclear[n]=false;
          rooms[numrooms].x=rand()%70+10;
          rooms[numrooms].y=rand()%70+10;
          rooms[numrooms].w=rand()%12 + 8;
          rooms[numrooms].h=rand()%12 + 8;
            debugint("new room x", rooms[numrooms].x);
            debugint("new room y", rooms[numrooms].y);
            debugint("new room w", rooms[numrooms].w);
            debugint("new room h", rooms[numrooms].h);
          debug("resetting loop counter");
          n=-1;
        
      } // end of room did collide else
      
    }//end of num rooms loop
    //Check if all rooms cleared.
    roomtotallyclear=true;
    for (int n=0;n<numrooms;n++)
    {
      if (roomsclear[n]==false) roomtotallyclear=false;
    }
  } while (!roomtotallyclear); //If all rooms are not clear, run again.
  
  //If it makes it here, it is in currently open space and ready to be placed and drawn
  //Loop through the terrain objects for the room and actually draw it now
  
  //Select a foreground and background color
  rooms[numrooms].bcolor = rand()%7 + 1; //1 - 7
  rooms[numrooms].fcolor = rand()%8 + 9; //9 - 16
  
  //Loop through data[x][y] and change the colors and make walkable
  for (int x=rooms[numrooms].x;x<rooms[numrooms].x+rooms[numrooms].w;x++)
  {
    for (int y=rooms[numrooms].y;y<rooms[numrooms].y+rooms[numrooms].h;y++)
    {
      data[x][y].fcolor = rooms[numrooms].fcolor;
      data[x][y].bcolor = rooms[numrooms].bcolor;
      data[x][y].walkable = true;
      itoa(numrooms,temp,10);
      data[x][y].shape = temp[0];
    }// end of y for
  }// end of x for
  
  
  numrooms++;
}

bool board::buildHallway(room one, room two) //Builds a hallway between the two rooms
{ //Returns true if it does, returns false if it can't
  
}

void board::draw() //This function initially draws a board, do not use this to update a board
{
  for (int x=0;x<80;x++) //Size of a standard dos window
  {
    for (int y=0;y<25;y++) //Size of a standard dos window
    {
      gotoxy(x,y); //Moves cursor to this x,y position
      setColor(data[x][y].color()); //Sets the output color to the proper color as held in the terrain object
      cout << data[x][y].shape; //Outputs the character as held in the terrain object
    }
  }
  gotoxy(0,0); //Moves back to the top left at the end
}


//ROOM OBJECT SECTION

room::room(int nx, int ny, int nw, int nh)
{
  x = nx;
  y = ny;
  w = nw;
  h = nh;
}

room::room()
{
  x = -1;
  y = -1;
  w = -1;
  h = -1;
}
